package com.tcheung;

import com.chenlb.mmseg4j.*;
import com.tcheung.entity.Document;
import com.tcheung.entity.TFEntity;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 分词类
 *
 * @author: zhangteng
 * @time: 2014/11/29 18:49
 */
public class WordSeg {

    private static Dictionary dictionary;

    public WordSeg() {
        dictionary = Dictionary.getInstance();
    }

    public void segWords(Document document) {
        Map<String, Integer> wordCountMap = new LinkedHashMap<String, Integer>();
        Seg seg = new ComplexSeg(dictionary);
        MMSeg mmSeg = new MMSeg(new StringReader(document.getContent()), seg);
        Word word = null;
        String w = "";
        Integer wordCount;
        Integer maxWordCount = 0;
        try {
            while ((word = mmSeg.next()) != null) {
                w = word.getString();
                if (wordCountMap.containsKey(w)) {
                    wordCount = wordCountMap.get(w);
                } else {
                    wordCount = 0;
                }
                wordCountMap.put(w, wordCount + 1);
                if (maxWordCount.compareTo(wordCount + 1) < 0) {
                    maxWordCount = wordCount + 1;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        int docNum = document.getDocNum();
        for (Map.Entry<String, Integer> entry : wordCountMap.entrySet()) {
            TFEntity tfEntity = new TFEntity(docNum, entry.getKey(), entry.getValue());
            document.addTFEntity(tfEntity);
        }
        document.setMaxTermCount(maxWordCount);
    }

    public static List<TFEntity> segWords(String text) {
        Map<String, Integer> wordCountMap = new LinkedHashMap<String, Integer>();
        Seg seg = new ComplexSeg(dictionary);
        MMSeg mmSeg = new MMSeg(new StringReader(text), seg);
        Word word = null;
        String w = "";
        Integer wordCount;
        Integer maxWordCount = 0;
        try {
            while ((word = mmSeg.next()) != null) {
                w = word.getString();
                if (wordCountMap.containsKey(w)) {
                    wordCount = wordCountMap.get(w);
                } else {
                    wordCount = 0;
                }
                wordCountMap.put(w, wordCount + 1);
                if (maxWordCount.compareTo(wordCount + 1) < 0) {
                    maxWordCount = wordCount + 1;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        List<TFEntity> tfEntityList = new ArrayList<TFEntity>();
        Double mCount = Double.valueOf(maxWordCount);
        for (Map.Entry<String, Integer> entry : wordCountMap.entrySet()) {
            TFEntity tfEntity = new TFEntity(-1, entry.getKey(), entry.getValue(), entry.getValue() / mCount);
            tfEntityList.add(tfEntity);
        }
        return tfEntityList;
    }
}
